<?php
include_once 'application/controllers/AbstractController.php';
require_once 'application/models/Menu.php';
require_once 'application/models/MenuPrincipal.php';
require_once 'application/models/SousMenu.php';
require_once 'application/models/Utilisateur.php';

class MainMenuController extends AbstractController 
{
	private static $_VISIBILITY_PUBLIC_READ = 0;
	private static $_VISIBILITY_PUBLIC_WRITE = 1;
	private static $_VISIBILITY_PRIVATE = 2;
	
	//TODO: liste des exercices de la série ?
	function indexAction()
    {
		parent::indexAction();
    	return $this->getAction();
    }
    
    
    function getAction()
    {
		parent::getAction();
    	//TODO: check rights pour chaque fonction (Zend_Acl ?)
		
		// get params
		$id = $this->getRequest()->getParam('id');
		if ($id == 'private') {
			// get my own menu
			if ($this->user->private_menu_id <= 0) {
				HttpUtil::do404();
			}

			$table = new Menu();
			$where = $table->getAdapter()->quoteInto('id = ?', $this->user->private_menu_id);
		    $menuRows = $table->fetchAll($where);
		    
	    	$menu = $table->getObjectFromRow($menuRows[0]);
		    
		    if (!isset($menu) || !isset($menu['menu'])) {
				HttpUtil::do404();
		    }
		    
		    $this->view->data = json_encode($menu['menu']);
		}else{
			// get menu from id
		    $table = new Menu();
			$whereId = $table->getAdapter()->quoteInto('id = ?', $id);
			$whereViz = $table->getAdapter()->quoteInto('visibility = ?', self::$_VISIBILITY_PUBLIC_READ);
			$wherePrivate = $table->getAdapter()->quoteInto('id = ?', $this->user->private_menu_id);
			$menuRows = $table->fetchAll(array($whereId, $whereViz.' OR '.$wherePrivate));
		    
	    	$menu = $table->getObjectFromRow($menuRows[0]);
	    	
		    if (!isset($menu) || !isset($menu['menu'])) {
				HttpUtil::do404();
		    }
		    
		    $this->view->data = json_encode($menu['menu']);
		}
    }
    
    function listAction()
    {
		//TODO: check rights pour chaque fonction (Zend_Acl ?)
		//TODO: -> chopper que nos menus ou les public (visibility = public, user_group, perso, etc.)
		
	    $table = new Menu();
		$where = $table->getAdapter()->quoteInto('visibility = ?', self::$_VISIBILITY_PUBLIC_READ);
	    $menuRows = $table->fetchAll($where);
    	
	    if (!isset($menuRows[0])) {
			HttpUtil::do404();
	    }
	    $menus = array();
		for ($i = 0; $i < count($menuRows); $i++) {
    		$menus[] = $table->getArrayFromRow($menuRows[$i]);
		}
	    
	    $this->view->data = json_encode($menus);
    }
    
    
    
    function postAction()
    {
		parent::postAction();
		// get params
		$doReset = $this->getRequest()->getParam('reset');

		if (isset($doReset)) {
			$this->reset();
		}else{
			HttpUtil::do405();
		}
    }

    private function reset() {
		// delete my own menu
		$table = new Menu();
		if ($this->user->private_menu_id > 0) {
			try {
				$menuRows = $table->find($this->user->private_menu_id);
				$currentMenu = $menuRows->current();
				$currentMenu->delete();
			}catch(Exception $exception) {
				//TODO: rollback !!!
				HttpUtil::do400('delete_failed');
			}
		}

		// create a new one
		try {
			$data = array(
				'nom' => 'Mon menu',
				'visibility' => self::$_VISIBILITY_PRIVATE
			);
			 
			$lastInsertId = $table->insert($data);	    
		}catch(Exception $exception) {
			HttpUtil::do400('creation_failed');
		}

		// set it as my own private menu
		try {
			$tableU = new Utilisateur();
			$data = array(
				'private_menu_id' => $lastInsertId
			);
			 
			$where = $tableU->getAdapter()->quoteInto('id = ?', $this->user->id);

			$numberOfRows = $tableU->update($data, $where);
		}catch(Exception $exception) {
			HttpUtil::do400('update_failed');
		}
		
	    $this->view->data = json_encode(array('id' => $lastInsertId));
	
	    // update user
		$where = $tableU->getAdapter()->quoteInto('id = ?', $this->user->id);
	    $userRows = $tableU->fetchAll($where);
		if (isset($userRows[0])) {
			Zend_Auth::getInstance()->getStorage()->write($userRows[0]);
		}
    }
    
    
    function putAction()
    {
		parent::putAction();
		$menuId = $this->getRequest()->getParam('id');
		
		if ($menuId == 'private'){
			//set private menu
			$menuId = $this->user->private_menu_id;
		}
		//public menu
		$table = new Utilisateur();
		try {
			$data = array(
				'menu_id' => $menuId
			);
			 
			$where = $table->getAdapter()->quoteInto('id = ?', $this->user->id);

			$numberOfRows = $table->update($data, $where);
		}catch(Exception $exception) {
			HttpUtil::do400('update_failed');
		}
		
		$where = $table->getAdapter()->quoteInto('id = ?', $this->user->id);
	    $userRows = $table->fetchAll($where);
		if (isset($userRows[0])) {
			Zend_Auth::getInstance()->getStorage()->write($userRows[0]);
		}
	}
    
    
    
    function deleteAction()
    {
		parent::deleteAction();
    	HttpUtil::do405();
    }
}
